package com.yuniot.todoapp

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText


class TestActivity : AppCompatActivity() {

    // 定义预设的登录凭据
    private val VALID_USERNAME = "10086"
    private val VALID_PASSWORD = "abc123"

    // UI 组件
    private lateinit var etUsername: TextInputEditText
    private lateinit var etPassword: TextInputEditText
    private lateinit var btnLogin: MaterialButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        // 初始化 UI 组件
        initViews()

        // 设置点击事件
        setupClickListeners()
    }

    private fun initViews() {
        etUsername = findViewById(R.id.etUsername)
        etPassword = findViewById(R.id.etPassword)
        btnLogin = findViewById(R.id.btnLogin)
    }

    private fun setupClickListeners() {
        btnLogin.setOnClickListener {
            performLogin()
        }
    }

    private fun performLogin() {
        // 获取输入的用户名和密码
        val inputUsername = etUsername.text.toString().trim()
        val inputPassword = etPassword.text.toString().trim()

        // 检查输入是否为空
        if (inputUsername.isEmpty()) {
            showErrorDialog("请输入用户名")
            return
        }

        if (inputPassword.isEmpty()) {
            showErrorDialog("请输入密码")
            return
        }

        // 验证用户名和密码
        if (inputUsername == VALID_USERNAME && inputPassword == VALID_PASSWORD) {
            showSuccessDialog()
        } else {
            showFailureDialog()
        }
    }

    private fun showSuccessDialog() {
        AlertDialog.Builder(this)
            .setTitle("登录成功")
            .setMessage("欢迎您，登录成功！")
            .setIcon(android.R.drawable.ic_dialog_info)
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
                // 这里可以添加登录成功后的逻辑
                navigateToMainActivity()
            }
            .setCancelable(false)
            .show()
    }

    private fun navigateToMainActivity() {
        // 跳转到主页面 - 请将 MainActivity 替换为您的目标页面
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish() // 关闭当前登录页面，防止用户按返回键回到登录页
    }

    private fun showFailureDialog() {
        AlertDialog.Builder(this)
            .setTitle("登录失败")
            .setMessage("用户名或密码错误，请重新输入")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("重试") { dialog, _ ->
                dialog.dismiss()
                // 清空密码输入框
                etPassword.setText("")
                etPassword.requestFocus()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }
            .show()
    }

    private fun showErrorDialog(message: String) {
        AlertDialog.Builder(this)
            .setTitle("输入错误")
            .setMessage(message)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
            }
            .show()
    }
}